﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using CodeFarmer.Model.Entities;
using CodeFarmer.Model.Enumeration;
using CodeFarmer.Service;
using CodeFarmer.Core.Utility;

namespace CodeFarmer.Controller.Controller
{
    public class UserCenterController : BaseController
    {
        private readonly IUserService userService;

        public UserCenterController(IUserService userService)
        {
            this.userService = userService;
        }

        [HttpGet]
        public ActionResult Register()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        public ActionResult Logoff()
        {
            UserContext.Instance.SignOut();
            return RedirectToAction("Index", "Site");
        }

        [HttpPost]
        public ActionResult Register(User user)
        {
            if (ModelState.IsValid)
            {
                user.CreateTime = DateTime.Now;
                user.LastLoginTime = DateTime.Now;
                user.State = (int)AccountState.Normal;
                user.PassWord = CryptoHelper.MD5_Encrypt(user.PassWord, "utf-8");
                user.LastLoginIP = Request.UserHostAddress;
                int effect = userService.AddUser(user);
                if (effect > 0)
                {
                    UserContext.Instance.SignIn(user.UserName, false);
                    return RedirectToAction("Index", "Site");
                }
                else
                {
                    return RedirectToAction("Login");
                }
            }

            return View(user);

        }

        [HttpPost]
        public ActionResult Login(User user, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                string password = CryptoHelper.MD5_Encrypt(user.PassWord, "utf-8");
                if (userService.ValidateUser(user.UserName, password))
                {
                    bool rememberMe = false;
                    Boolean.TryParse(Request.Form.GetValues("RememberMe")[0], out rememberMe);
                    UserContext.Instance.SignIn(user.UserName, rememberMe);
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Site");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "用户名或密码不正确。");
                }
            }
            return View(user);
        }

        [HttpPost]
        public ActionResult CheckEmail()
        {
            bool isExist = userService.ExistEmail(Request["mail"]);
            if (isExist)
                return Content("false");
            else
                return Content("true");
        }

        [HttpPost]
        public ActionResult CheckUserName()
        {
            bool isExist = userService.ExistUserName(Request["UserName"]);
            if (isExist)
                return Content("false");
            else
                return Content("true");
        }

        [HttpPost]
        public ActionResult CheckNickName()
        {
            bool isExist = userService.ExistNickName(Request["NickName"]);
            if (isExist)
                return Content("false");
            else
                return Content("true");
        }

        [HttpPost]
        public ActionResult CheckAuthenCode()
        {
            if (Request["AuthenCode"] != Session["authencode"].ToString())
                return Content("false");
            else
                return Content("true");
        }

        [HttpGet]
        public ActionResult GetUser(int id)
        {
            User user = userService.GetUser(id);
            return View(user);
        }
    }
}
